package system_storage_service

import (
	"time"

	"gitee.com/Luna-CY/Golang-Project-Template/internal/configuration"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/context"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/logger"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/util/pointer"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
)

func (cls *Storage) PreDeleteObject(ctx context.Context, object *model.SystemObject) errors.Error {
	if nil == object {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("ISS_GE.S_GE.PDO_CT.121958 object is nil")

		return errors.ErrorServerInternalError("ISS_GE.S_GE.PDO_CT.142019", "异常的传入参数")
	}

	if model.SystemObjectStatusUploaded != *object.Status {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("ISS_GE.S_GE.PDO_CT.182049 object status is %v", *object.Status)

		return errors.ErrorServerInternalError("ISS_GE.S_GE.PDO_CT.202056", "数据错误")
	}

	object.Status = pointer.New(model.SystemObjectStatusPreDelete)

	// 设置计划删除时间
	object.PlanDeleteTime = pointer.New(time.Now().Add(time.Hour * 24 * time.Duration(configuration.Configuration.Storage.PreDeleteLife)).Unix())

	if err := cls.object.SaveSystemObject(ctx, object); err != nil {
		return err.Relation(errors.ErrorServerInternalError("ISS_GE.S_GE.PDO_CT.272117", "保存对象失败"))
	}

	return nil
}
